package com.example.bee_work_3.fragment.home;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.bee_work_3.R;
import com.example.bee_work_3.webc_ontent.IntentCallBack;
import com.example.bee_work_3.fragment.ListItem;

import java.util.List;

public class PicturePagerAdapter extends PagerAdapter {

    private List<ListItem> mList;
    private Context mContext;
    private IntentCallBack callBack;

    public PicturePagerAdapter(IntentCallBack callBack, List<ListItem> list, Context context) {
        this.mList = list;
        this.mContext = context;
        this.callBack = callBack;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final ListItem item = mList.get(position);
        ImageView imageView = new ImageView(mContext);
        Glide.with(mContext)
                .load(item.getImage())
                .placeholder(R.mipmap.loading)
                .into(imageView);
        container.addView(imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.onIntent(item);
            }
        });

        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        //super.destroyItem(container,position,object);
    }


}